package com.txzt.gaea.upgrade.result.service.impl;

import com.txzt.gaea.common.base.service.impl.BaseServiceImpl;
import com.txzt.gaea.common.enums.BorrowStatus;
import com.txzt.gaea.common.enums.FlowFlagEnum;
import com.txzt.gaea.common.enums.IsDeleteEnum;
import com.txzt.gaea.flow.service.FlowService;
import com.txzt.gaea.upgrade.result.domain.model.ResultBorrow;
import com.txzt.gaea.upgrade.result.service.IResultBorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class ResultBorrowServiceImpl extends BaseServiceImpl<ResultBorrow> implements IResultBorrowService {


    @Autowired
    private FlowService flowService;

    /**
     * 成果申请 - 保存
     * @param resultBorrow
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int addResultBorrow(ResultBorrow resultBorrow) {
        resultBorrow.setBorrowStatus(BorrowStatus.CHECK.getCode());
        resultBorrow.setIsDelete(IsDeleteEnum.NOTDELETE.getCode());
        int count = sqlManager.insertTemplate(resultBorrow,true);
        Long flowId = flowService.startFlowUpgrade(resultBorrow.getId(), "tb_result_borrow","borrow_status",
                BorrowStatus.BACK.getCode(),BorrowStatus.PASS.getCode(), BorrowStatus.REJECT.getCode(),
                BorrowStatus.REJECT.getCode(),FlowFlagEnum.RESULT_BORROW.getCode(), resultBorrow.getDeptId());
        resultBorrow.setFlowId(flowId);
        sqlManager.updateTemplateById(resultBorrow);
        return count;
    }

    /**
     * 查询待审核数量(成果借阅管理)
     * @param resultBorrow
     * @return
     */
    @Override
    public Long listCheckCount(ResultBorrow resultBorrow) {
        List<Long> list = sqlManager.select("resultBorrow.selectResultBorrowCheckCount", Long.class, resultBorrow);
        return list.get(0);
    }
}
